package tienmanh.gs.goldminer.sprite.shop;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;

import tienmanh.gs.goldminer.manager.ResourcesManager;
import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.sprite.Explode;
import tienmanh.gs.goldminer.sprite.Hook;
import tienmanh.gs.goldminer.sprite.MyText;
import tienmanh.gs.goldminer.util.Util;

public class Boom extends AnimatedSprite implements ITouchArea{

	Line line;
	Hook hook;
	Boom instance;
	ITiledTextureRegion tiled;
	VertexBufferObjectManager vbom;
	Util util;
	public Boom(float pX, float pY,float w,float h,
			ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager, Line line,Hook hook) {
		super(pX, pY,w,h, pTiledTextureRegion, pVertexBufferObjectManager);
		// TODO Auto-generated constructor stub
		tiled = pTiledTextureRegion;
		vbom = pVertexBufferObjectManager;
		util = new Util();
		this.line = line;
	    this.hook = hook;
	}
	public void moveText( float paramFloat3, float paramFloat4 , final int value)
	  {
	    final SequenceEntityModifier entityModifier = new SequenceEntityModifier(
				new IEntityModifierListener() {
					@Override
					public void onModifierStarted(
							final IModifier<IEntity> pModifier,
							final IEntity pItem) {
					}
					@Override
					public void onModifierFinished(
							final IModifier<IEntity> pEntityModifier,
							final IEntity pEntity) {
						GameScene.sceneHUD.detachChild(GameScene.arrayGolds.get(value));
//						GameScene.arrayGolds.remove(value);
						GameScene.arrayGolds.set(value, null);
					    GameScene.sceneHUD.detachChild(Boom.this);
					    GameScene.getInstace().isPull = false;
					    hook.setVisible(true);
					    hook.unPullHook();
						hook.Rotate();
//						GameScene.hook.updatePosition(MathUtils.random(2, 175));
						GameScene.miner.stopAnimation();
						if(GameScene.isMinerRunning)
							GameScene.miner.MinerRun();
						GameScene.pullGold = false;
              			GameScene.getInstace().isTouch = true;
              			GameScene.getInstace().waitingThrowHook(hook);
              			GameScene.mines++;
            			GameScene.item.writeItemSP2(GameScene.mines);
            			GameScene.tvCountMine.setText(""+GameScene.mines);
					}
				}, new PathModifier(1.0F, new PathModifier.Path(3).to(getX(), getY()).to(300, paramFloat4).to(paramFloat3, paramFloat4)));
		    registerEntityModifier(entityModifier);
	  }
	@Override
	public boolean onAreaTouched(TouchEvent arg1,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		if(GameScene.getInstace().isPull && arg1.getAction() == TouchEvent.ACTION_DOWN && !GameScene.getInstace().isUnPull && GameScene.mines > 0 ){
			GameScene.mines--;
			GameScene.item.writeItemSP2(GameScene.mines);
			GameScene.tvCountMine.setText(""+GameScene.mines);
//			GameScene.miner.setAnimateThrow();
			int x = util.getCenterX(GameScene.arrayGolds.get(GameScene.getInstace().t));
            int y = util.getCenterY(GameScene.arrayGolds.get(GameScene.getInstace().t));
			  Explode explode = new Explode(0.0F, 0.0F, 100.0F, 100.0F, ResourcesManager.textureRegionExplode, vbom){
              	@Override
              	protected void onManagedUpdate(float pSecondsElapsed) {
              		// TODO Auto-generated method stub
              		super.onManagedUpdate(pSecondsElapsed);
              		if(!isAnimationRunning()) this.setVisible(false);
              	}
              };
              explode.setPosition(-50+x, -50+y);
              explode.setWidth(100.0F);
              explode.setHeight(100.0F);
              GameScene.sceneHUD.attachChild(explode);
              explode.restart();
							    GameScene.getInstace().inExplode = true;
								GameScene.sceneHUD.detachChild((GameScene.arrayGolds.get(GameScene.getInstace().t)));
								GameScene.arrayGolds.remove(GameScene.getInstace().t);
								GameScene.getInstace().pullHookState();
								
			
		}
		return super
				.onAreaTouched(arg1, pTouchAreaLocalX, pTouchAreaLocalY);
	}

}